import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();

        for (String token : tokens) {
            switch (token) {
                case "+":
                    stack.push(stack.pop() + stack.pop());
                    break;
                case "-":
                    int b = stack.pop();
                    int a = stack.pop();
                    stack.push(a - b);
                    break;
                case "*":
                    stack.push(stack.pop() * stack.pop());
                    break;
                case "/":
                    int divisor = stack.pop();
                    int dividend = stack.pop();
                    stack.push(dividend / divisor);
                    break;
                default:
                    stack.push(Integer.parseInt(token));
            }
        }

        return stack.pop();
    }

    // 主方法：程序入口点
    public static void main(String[] args) {
        Solution solution = new Solution();

        // 测试用例1: 2 + 1 * 3 = 5
        String[] tokens1 = {"2", "1", "3", "*", "+"};
        int result1 = solution.evalRPN(tokens1);
        System.out.println("表达式: 2 1 3 * +");
        System.out.println("计算结果: " + result1); // 输出5

        // 测试用例2: (3 + 4) * 5 = 35
        String[] tokens2 = {"3", "4", "+", "5", "*"};
        int result2 = solution.evalRPN(tokens2);
        System.out.println("\n表达式: 3 4 + 5 *");
        System.out.println("计算结果: " + result2); // 输出35

        // 测试用例3: 复杂表达式
        String[] tokens3 = {"4", "13", "5", "/", "+"};
        int result3 = solution.evalRPN(tokens3);
        System.out.println("\n表达式: 4 13 5 / +");
        System.out.println("计算结果: " + result3); // 输出6
    }
}